package com.ken.blog.controller.admin;

import com.ken.blog.domain.Category;
import com.ken.blog.domain.CategoryRepository;
import com.ken.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("admin/category")
public class AdminCategoryController {
    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("admin/category/index");

        List<Category> list = this.categoryRepository.findAll();

        mv.addObject("list", list);

        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("admin/category/add");

        if (request.getMethod().equals("POST")) {
            Category category = new Category();
            category.setTitle(request.getParameter("title"));
            category.setDescription(request.getParameter("description"));

            Category result = categoryService.saveCategory(category);
            System.out.println(result);

            mv.addObject("message", "添加成功");
            mv.addObject("url", "/admin/category");
            mv.setViewName("admin/message");
        }

        return mv;
    }

    @RequestMapping("/edit/{id}")
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
        ModelAndView mv = new ModelAndView("admin/category/edit");

        Category category = this.categoryRepository.findOne(id);
        if (category == null) {
            mv.addObject("message", "不存在的分类");
            mv.addObject("url", "/admin/category");
            mv.setViewName("admin/message");
            return mv;
        }

        if (request.getMethod().equals("POST")) {
            String title = request.getParameter("title");
            String description = request.getParameter("description");
            int result = this.categoryRepository.update(title, description, id);

            System.out.println(result);

            mv.addObject("message", "编辑成功");
            mv.addObject("url", "/admin/category");
            mv.setViewName("admin/message");
            return mv;
        }

        mv.addObject("category", category);

        return mv;
    }

    @RequestMapping("/delete/{id}")
    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
        ModelAndView mv = new ModelAndView("admin/message");

        Category category = this.categoryRepository.findOne(id);
        if (category == null) {
            mv.addObject("message", "不存在的分类");
            mv.addObject("url", "/admin/category");
            return mv;
        }

        this.categoryRepository.delete(id);

        mv.addObject("message", "删除成功");
        mv.addObject("url", "/admin/category");
        return mv;
    }
}
